package com.sitr.service.impl;

import com.sitr.mapper.UsersMapper;
import com.sitr.pojo.Users;
import com.sitr.service.UserService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @Description ServiceUser
 * @Author Sitr
 * @Date 2021/3/2 2:46
 * @Version 1.0
 **/

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UsersMapper userMapper;

    @Autowired
    private Sid sid;

    @Override
    public boolean queryUsernameIsExist(String username) {

        Users user = new Users();
        user.setUsername(username);

        Users result = userMapper.selectOne(user);



        return result != null ?true : false;
    }

    @Transactional(propagation =  Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String pwd) {

        Example userExample = new Example(Users.class);
        Example.Criteria criteria = userExample.createCriteria();

        criteria.andEqualTo("username",username);
        criteria.andEqualTo("password",pwd);

        Users result = userMapper.selectOneByExample(userExample);

        return result;
    }

    @Override
    public Users saveUser(Users user) {

        String userId = sid.nextShort();


        user.setQrcode("");
        user.setId(userId);
        userMapper.insert(user);
        return user;
    }
}
